package com.dadaban.controller;

import com.dadaban.UserConstant;
import com.dadaban.UserTool;
import com.dadaban.model.UserAddress;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liu_yan on 2018/9/18.
 */
@RestController
@RequestMapping(value="/userAddress")
public class UserAddressController {
    private Logger LOGGER = LoggerFactory.getLogger(UserAddressController.class);
    @Autowired
    private UserAddressRepository userAddressRepository;

    /**
     * 根据usrUid查询用户地址信息
     * @return
     */
    @PostMapping(value = "/getUserAddresss/{usrUid}")
    public Map<String, Object> getUserAddresssInfo(@PathVariable("usrUid") String usrUid,
    		@RequestParam(value = "addTyp", required = false) String addTyp){
    	LOGGER.info("getUserAddresssInfo 根据usrUid查询用户地址信息");
    	Map<String, Object> resultMap = new HashMap<String, Object>();
    	UserAddress userAdd = new UserAddress();
        userAdd.setUsrUid(usrUid);
        userAdd.setSts("A");
        if(addTyp != null){
        	userAdd.setAddTyp(addTyp);
        }
    	Example<UserAddress> example = Example.of(userAdd);
    	if(userAddressRepository.findAll(example) == null){
    		resultMap.put(UserConstant.ErrorNo, UserConstant.FAIL);
        	resultMap.put(UserConstant.ErrorMsg, "根据usrUid查询用户地址信息出错");
        	return resultMap;
    	}
    	List<UserAddress> userAddList = userAddressRepository.findAll(example);
    	resultMap.put("userAddInfo", userAddList);
    	resultMap.put(UserConstant.ErrorNo, UserConstant.SUCCESS);
    	resultMap.put(UserConstant.ErrorMsg, "");
    	return resultMap;
    }
    
    /**
     * 新增用户地址信息
     * @return
     */
    @PostMapping(value = "/addUserAddress")
    public Map<String, Object> addUserAddress(@RequestParam(value = "usrUid", required = true) String usrUid,
    		@RequestParam(value = "addNam", required = true) String addNam,
    		@RequestParam(value = "addOrd", required = true) Integer addOrd,
    		@RequestParam(value = "addLon", required = false) BigDecimal addLon,
    		@RequestParam(value = "addLat", required = false) BigDecimal addLat,
    		@RequestParam(value = "addTyp", required = false, defaultValue="QT") String addTyp){
        LOGGER.info("addUserAddress 新增用户地址信息");
        /*
         * 必填字段校验
         */
        Map<String, Object> resultMap = new HashMap<String, Object>();
    	resultMap.put(UserConstant.ErrorNo, UserConstant.FAIL);
    	resultMap.put(UserConstant.ErrorMsg, "新增用户地址信息出错");
        if(usrUid == null || "".equals(usrUid.trim())){
        	LOGGER.error("addUserAddress 新增用户地址信息出错，缺少用户主键usrUid");
        	return resultMap;
        }
        if(addNam == null || "".equals(addNam.trim())){
        	LOGGER.error("addUserAddress 新增用户地址信息出错，缺少地址名addNam");
        	return resultMap;
        }
        if(addOrd == null){
        	LOGGER.error("addUserAddress 新增用户地址信息出错，缺少排序号addOrd");
        	return resultMap;
        }
        
        /*
         * 新增赋值
         */
        UserAddress userAdd = new UserAddress();
        userAdd.setUsrUid(usrUid);
        userAdd.setAddOrd(addOrd);
        userAdd.setAddNam(addNam);
        userAdd.setAddLon(addLon);
        userAdd.setAddLat(addLat);
        userAdd.setAddTyp(addTyp);
        userAdd.setSts("A");
        Date d = new Date();
        SimpleDateFormat ddf = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat tdf = new SimpleDateFormat("kkmmss");
        userAdd.setCrtDte(ddf.format(d));
        userAdd.setCrtTim(tdf.format(d));
        userAddressRepository.save(userAdd);
        
        resultMap.put("userAddress", userAdd);
    	resultMap.put(UserConstant.ErrorNo, UserConstant.SUCCESS);
    	resultMap.put(UserConstant.ErrorMsg, "");
    	return resultMap;
    }
    
    /**
     * 根据addUid修改用户地址信息
     * @return
     */
    @PutMapping(value = "/updateUserAddress/{addUid}")
    public Map<String, Object> updateUserAddress(@PathVariable("addUid") String addUid,
    		@RequestParam(value = "addNam", required = false) String addNam,
    		@RequestParam(value = "addOrd", required = false) Integer addOrd,
    		@RequestParam(value = "addLon", required = false) BigDecimal addLon,
    		@RequestParam(value = "addLat", required = false) BigDecimal addLat,
    		@RequestParam(value = "addTyp", required = false) String addTyp){
    	LOGGER.info("updateUserAddress 根据addUid修改用户地址信息");
    	Map<String, Object> resultMap = new HashMap<String, Object>();
    	/*
    	 * 校验
    	 */
    	UserAddress userAdd = new UserAddress();
    	userAdd.setAddUid(addUid);
    	userAdd.setSts("A");
    	Example<UserAddress> example = Example.of(userAdd);
    	if(userAddressRepository.findOne(example).orElse(null) == null){
    		resultMap.put(UserConstant.ErrorNo, UserConstant.FAIL);
        	resultMap.put(UserConstant.ErrorMsg, "根据addUid修改用户地址信息出错");
        	return resultMap;
    	}
    	UserAddress source = userAddressRepository.findOne(example).get();
    	
    	/*
    	 * 用户地址数据更新
    	 */
    	userAdd.setAddNam(addNam);
        userAdd.setAddOrd(addOrd);
        userAdd.setAddLon(addLon);
        userAdd.setAddLat(addLat);
        userAdd.setAddTyp(addTyp);
    	Date d = new Date();
    	SimpleDateFormat ddf = new SimpleDateFormat("yyyyMMdd");
    	SimpleDateFormat tdf = new SimpleDateFormat("kkmmss");
    	userAdd.setRecDte(ddf.format(d));
    	userAdd.setRecTim(tdf.format(d));
        UserTool.copyNullProperties(source, userAdd);
        userAddressRepository.save(userAdd);
        
        resultMap.put("userAddress", userAdd);
    	resultMap.put(UserConstant.ErrorNo, UserConstant.SUCCESS);
    	resultMap.put(UserConstant.ErrorMsg, "");
    	return resultMap;
    }
    
    /**
     * 根据addUid删除用户地址信息
     * @return
     */
    @DeleteMapping(value = "/deleteUserAddress/{addUid}")
    public Map<String, Object> deleteUserAddress(@PathVariable("addUid") String addUid){
    	LOGGER.info("deleteUserAddress 根据addUid删除用户地址信息");
    	Map<String, Object> resultMap = new HashMap<String, Object>();
    	/*
    	 * 校验
    	 */
    	UserAddress userAdd = new UserAddress();
    	userAdd.setAddUid(addUid);
    	userAdd.setSts("A");
    	Example<UserAddress> example = Example.of(userAdd);
    	if(userAddressRepository.findOne(example).orElse(null) == null){
    		resultMap.put(UserConstant.ErrorNo, UserConstant.FAIL);
        	resultMap.put(UserConstant.ErrorMsg, "根据addUid删除用户地址信息出错");
        	return resultMap;
    	}
    	UserAddress source = userAddressRepository.findOne(example).get();
    	
    	userAdd.setSts("C");
    	Date d = new Date();
    	SimpleDateFormat ddf = new SimpleDateFormat("yyyyMMdd");
    	SimpleDateFormat tdf = new SimpleDateFormat("kkmmss");
    	userAdd.setRecDte(ddf.format(d));
    	userAdd.setRecTim(tdf.format(d));
        UserTool.copyNullProperties(source, userAdd);
        userAddressRepository.save(userAdd);
        
        resultMap.put("userAddress", userAdd);
    	resultMap.put(UserConstant.ErrorNo, UserConstant.SUCCESS);
    	resultMap.put(UserConstant.ErrorMsg, "");
    	return resultMap;
    }
    
}
